from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers

from goods.models import SKU, Brand, GoodsChannel, GoodsCategory, Goods, GoodsSpecification, SpecificationOption, \
    SKUImage
from goods.search_indexes import SKUIndex


class SKUSerializer(serializers.ModelSerializer):
    """
    SKU序列化器
    """

    class Meta:
        model = SKU
        fields = ('id', 'name', 'price', 'default_image', 'comments')


class SKUIndexSerializer(HaystackSerializer):
    """
    SKU索引结果数据序列化器
    """
    object = SKUSerializer(read_only=True)

    class Meta:
        index_classes = [SKUIndex]
        fields = ('text', 'object')


class BrandSerializer(serializers.ModelSerializer):
    class Meta:
        model = Brand
        fields = "__all__"


class GoodsChannelSerialiazer(serializers.ModelSerializer):
    category = serializers.StringRelatedField(read_only=True)
    category_id = serializers.IntegerField()
    group_id = serializers.IntegerField()
    group = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = GoodsChannel
        fields = "__all__"


class SPUCateSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ["id", "name"]


class GoodsSerializer(serializers.ModelSerializer):
    brand = serializers.StringRelatedField(read_only=True)
    category1 = serializers.StringRelatedField(read_only=True)
    category2 = serializers.StringRelatedField(read_only=True)
    category3 = serializers.StringRelatedField(read_only=True)

    brand_id = serializers.IntegerField()
    category1_id = serializers.IntegerField()
    category2_id = serializers.IntegerField()
    category3_id = serializers.IntegerField()

    class Meta:
        model = Goods
        fields = "__all__"


class GoodsSpecificationSerializer(serializers.ModelSerializer):
    """
    spu规格
    """
    spu_id = serializers.IntegerField()
    spu = serializers.StringRelatedField(read_only=True)

    class Meta:
        model = GoodsSpecification
        fields = "__all__"


class SpecificationOptionSerializer(serializers.ModelSerializer):
    spec = serializers.StringRelatedField(read_only=True)
    spec_id = serializers.IntegerField()

    class Meta:
        model = SpecificationOption
        fields = "__all__"


class SKUImageSerializer(serializers.ModelSerializer):
    sku = serializers.StringRelatedField(read_only=True)
    sku_id = serializers.IntegerField()

    class Meta:
        model = SKUImage
        fields = "__all__"


class SkuDefaultSerializer(serializers.ModelSerializer):
    category = serializers.StringRelatedField(read_only=True)
    category_id = serializers.IntegerField()

    class Meta:
        model = SKU
        fields = "__all__"

